﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class WareDisController : BaseController<waresdiscount>
    {
        //
        // GET: /WareDis/

        public override BLL.BaseBll<waresdiscount> bll
        {
            get
            {
                return new WaresdisBll();
            }
        }

        StoreBll storeBll = new StoreBll();

        /// <summary>
        /// 获取店铺列表
        /// </summary>
        /// <returns></returns>  

        public ActionResult GetShopName()
        {
            return Json(storeBll.Search().Select(x => new { shopID = x.storeid, shopName = x.storeName }).ToList());
        }

        /// <summary>
        /// 加载数据列表  waresDisID  waresCode  waresPrice storeName waresName
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult GetAll(int page, int rows, waresdiscount model)
        {
            int count;
            var list = bll.Search(model, x => true, true, x => x.waresDisID, rows, page, out count).ToList().Select(x => new
            {
                x.waresDisID,
                x.waresCode,
                x.waresPrice,
                x.storeid,
                x.store.storeName,
                x.wares.waresName,
            }).ToList();
            return Json(new { total = count, rows = list });
        }

        //新增优惠商品
        [HttpPost]
        public override ActionResult Save(waresdiscount model)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;
            if (model.waresCode != "" && model.waresCode != null && model.waresPrice != 0 && model.storeid != 0)
            {
                model.waresCode = model.waresCode.Replace(" ", "");
                //判断该用户是否存在  waresDisID
                if (bll.Add(model) > 0)
                    {
                    msg = "添加成功";
                    result = 1;
                    jieguo = 1;
                    return Json(new { jieguo = jieguo, state = result, msg = msg });
                }
                else
                {
                    jieguo = 1;
                    msg = "添加失败";
                    return Json(new { jieguo = jieguo, state = result, msg = msg });
                }
            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";
                return Json(new { jieguo = jieguo, state = result, msg = msg });
            }
        }

        //获取选中的一条数据
        public ActionResult GetOnlyOne(long id)
        {
            return Json(bll.Search(x => x.waresDisID == id).ToList().Select(x => new { x.storeid, x.waresCode,x.waresDisID, x.waresPrice ,x.store.storeName}).ToList()[0], JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除商品优惠信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //public  ActionResult Delete(long id)
        //{
        //    string msg = "";
        //    int result = 0;
        //    int jieguo = 1;
        //    if (bll.Delete(x=>x.waresDisID==id) > 0)
        //    {
        //        msg = "删除成功";
        //        result = 1;
        //        jieguo = 1;
        //        return Json(new { jieguo = jieguo, state = result, msg = msg });
        //    }
        //    else
        //    {
        //        jieguo = 1;
        //        msg = "删除失败";
        //        return Json(new { jieguo = jieguo, state = result, msg = msg });
        //    }
        //}

        /// <summary>
        /// 修改优惠信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public override ActionResult Modify(waresdiscount model)
        {
            string msg = "修改失败";
            string state = "false";
            //
            if (model.waresCode != "" && model.waresCode != null && model.waresPrice != 0 && model.storeid != 0)
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                return Json(new { state = state, msg = msg });
            }

            return Json(new { state = state, msg = msg });
        }

        public  ActionResult Delete(string ids)
        {
            long[] id = Array.ConvertAll<string, long>(ids.Split(','), x => long.Parse(x));
            string msg = "删除失败";
            string state = "false";
            if (bll.Delete(x => id.Contains(x.waresDisID)) > 0)
            {
                msg = "删除成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }

    }
}
